<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Model_Product extends Zend_Db_Table{
	
	protected $_name    = "product_buys";
    protected $_primary = "id";
    
    public function saveData($data){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            if(isset($data['id']) && $this->checkExists($data['id'])){
                $id = $data['id'];
                
                $db->update("product_buys", $data, "id = $id");
            }else{
                $db->insert('product_buys', $data);
            }
            
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function updateQuantity($data){
    	$db = Zend_Registry::get('db');
        
        $id = $data['id'];
        $product = $this->getData($id);
        $data['quantity'] += $product['quantity'];
        print_r($product);
        $db->update("product_buys", $data, "id = $id");
        
        return $product;
    }
    
    public function getDatas($options = null, $cols = array(),$join = true){
    	$db = Zend_Registry::get('db');
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        
        if(count($cols) == 0){
            $cols = array('id', 'name', 'net', 'price_sell as price', 'quantity');
        }
        
        $select = $db->select()
                    ->from(array('p' => 'product_buys'), $cols);  
        
        if($join){
            $select->join(array('b' => 'brands'), "p.brand = b.id", array('brand'));
        }
        
        if($options != null){
            foreach($options as $key => $value){
                $select->{$key}($value);
            }
        }
        
        if(($options != null && !array_key_exists('order', $options)) || $options == null){
            $select->order('p.brand')
                    ->order('p.name')
                    ->order('p.id');
        }
        
        $select->where('showinsale = 1');
        
        $result = $db->fetchAll($select);
        
        return $result;
    }
    
    public function getData($id){
    	if($this->checkExists($id)){
            $db = Zend_Registry::get('db');
            $db->setFetchMode(Zend_Db::FETCH_ASSOC);
            
            $select = $db->select()
                        ->from(array('p' => 'product_buys'), array('id', 'name', 'customer', 'brand', 'net', 'price', 'quantity'))
                        ->where('p.id = ?', $id, 'INTEGER');
            
            $rs = $db->fetchRow($select);
            
            return $rs;
        }
        
        return false;
    }
    
    public function deleteData($id){
    	$db = Zend_Registry::get('db');
        
        $db->beginTransaction();
        
        try{
            $db->delete("product_buys", "id = $id");
            $db->commit();
            
            return true;
        }catch(Exception $e){
            $db->rollBack();
            
            return false;
        }
    }
    
    public function checkExists($id){
        $option = array('table' => 'product_buys', 'field' => 'id');
    	$validator = new Zend_Validate_Db_RecordExists($option);
        
        if($validator->isValid($id)){
            return true;
        }
        
        return false;
    }
}